package com.app.view_controller;

import java.util.ArrayList;
import java.util.Calendar;

import com.app.alarm.R;
import com.app.model.AlarmDataManager;
import com.app.model.AlarmDataManager.AlarmLoader;
import com.app.service.AlarmService;
import com.app.util.ApplicationMediator;
import com.app.util.VolumeManager;
import com.app.view_controller.custom_view.TextViewWithFont;
import com.app.view_controller.custom_view.DayChooserWithFont;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ToggleButton;

@SuppressWarnings({ "unused", "deprecation" })
public class AlarmReceiverFragment extends Fragment implements OnClickListener {

	private static final String MESSAGE_LOADER = "messsage";
	private static final String TAG = "ALARM_RECEIVER_FRAGMENT";
	private static final boolean DEBUG = true;
	private static final float SHAKE_THRESHOLD = 1000;
	private static final long SHORT_DELAY = 200;
	private TextViewWithFont mTimeView;
	private ImageButton mSnoozeButton;
	private ImageView mLeftArrows;
	private ImageView mRightArrows;
	private TextViewWithFont mDismissTextView;
	private RelativeLayout mDismissTextContainer;
	private LinearLayout mContainer;
	private AlarmDataManager mAlarmData;
	private Ringtone tone;
	//	private VolumeManager audio;
	private int startVolumeLevel;
	private int defaultVolume;
	private ViewPager pager;
	private SensorManager sensorMgr;
	private long lastUpdate;
	private float last_x;
	private float last_y;
	private float last_z;
	private boolean isPostponed;

	public static AlarmReceiverFragment newInstance(String text) {

		AlarmReceiverFragment f = new AlarmReceiverFragment();
		Bundle b = new Bundle();
		b.putString(MESSAGE_LOADER, text);

		f.setArguments(b);

		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mAlarmData = AlarmService.getCurrentAlarm();
		
		isPostponed = false;

		if (mAlarmData.isRotationDisabled())
			getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

		if (DEBUG)
			Log.d(TAG, "OnreceiverFragment called with alarm " + mAlarmData.getAlarmId());
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		//Retrieve the ViewPager from the Activity container.
		pager = ((AlarmRecevierActivity) getActivity()).getViewPager();

		View v = inflater.inflate(R.layout.fragment_alarm_recevier, container, false);
		mTimeView = (TextViewWithFont) v.findViewById(R.id.receiver_time);
		mSnoozeButton = (ImageButton) v.findViewById(R.id.receiver_snooze_button);
		mContainer = (LinearLayout) v.findViewById(R.id.receiver_container);

		mSnoozeButton.setOnClickListener(this);

		pager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int pageNum) {
				if (pageNum != AlarmRecevierActivity.DEFAULT_PAGE)
					// Finish the activity after a small delay.
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							if (DEBUG)
								Log.d(TAG, "alarmservice created second " + AlarmService.isServiceCreated());
							Intent service = new Intent(getActivity(), AlarmService.class);
							service.setAction(AlarmService.STOP_ALARM_SERVICE);
							getActivity().startService(service);
							getActivity().finish();
						}
					}, SHORT_DELAY);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// YOU DO NOTHING, JON SNOW
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// YOU DO NOTHING, JON SNOW
			}
		});
		return v;
	}

	@Override
	public void onClick(View view) {
		Intent service = new Intent(getActivity(), AlarmService.class);
		service.setAction(AlarmService.POSTPONE_ALARM);
		getActivity().startService(service);
		getActivity().finish();
	}

	@Override
	public void onPause() {

		super.onPause();
	}
}
